package charts; 
import charts.series.Element;
import charts.series.bars.Horizontal;
import string.Utils;
import global.Global;
import charts.Structs.StyleStruct;

class HBar extends Base<Horizontal> {
	
	var group:Float;
	var style:StyleStruct;
	
	public function new( json:Dynamic ) {
		super();
/*		
		style = {
			values:				[],
			colour:				'#3030d0',
			text:				'',		// <-- default not display a key
			'font-size':		12,
			tip:				'#val#'
		};
*/
		style = {
			values		: new Array<Float>()
			, colour	: '#3030d0'
			, text		: '' // <-- default not display a key
			, font_size	: 12
			, tip		: '#val#'
		};

		Object_helper.merge_2( json, style );
		
		//this.alpha = Float( vals[0] );
		this.colour = string.Utils.get_colour( style.colour );
		this.key = json.text;
		this.font_size = json['font-size'];
		
		//
		// bars are grouped, so 3 bar sets on one chart
		// will arrange them selves next to each other
		// at each value of X, this.group tell the bar
		// where it is in that grouping
		//
		this.group = 0;
		
		this.values = json['values'];
		
		style.onclick = json['on-click'];
		
		this.add_values();
	}
	
	//
	// called from the base object, in this case the
	// value is the X value of the bar and the index
	// is the Y positiont
	//
	override function get_element( index:Float, value:Dynamic ): Element {
		
		var default_style:StyleStruct = {
				colour:		style.colour
				, onclick:	style.onclick
				, tip:		style.tip
		};
		
		if( Std.is( value, Float) )
			default_style.top = value;
		else
			Object_helper.merge_2( value, default_style );
			
		// our parent colour is a number, but
		// we may have our own colour:
		if( Std.is( default_style.colour, String) )
			default_style.colour = Utils.get_colour( default_style.colour );
		
		return new Horizontal( index, default_style, this.group );
	}
	
	public override function resize( sc:ScreenCoordsBase ): Void {
		
		for ( i in 0...this.numChildren)
		{
			var p:Horizontal = cast( this.getChildAt(i), Horizontal);
			p.resize( sc );
		}
	}
	
	public override function get_max_x():Float {
		
		var x:Int = 0;
		//
		// count the non-mask items:
		//
		//for (i:Int = 0; i < this.numChildren; i++ )
		for(i in 0...this.numChildren)
			if( Std.is( this.getChildAt(i), Horizontal) ) {
				
				var h:Horizontal = cast( this.getChildAt(i), Horizontal);
				x = Math.max( x, h.get_max_x() );
				
			}

		return x;
	}
	
	public override function get_min_x():Float {
		return 0;
	}

}
